Circular Linked List হল একটি লিঙ্কড লিস্টের একটি ভেরিয়েন্ট, যেখানে লিঙ্কড লিস্টের শেষ নোড প্রথম নোডের সাথে সংযুক্ত থাকে, অর্থাৎ এটি একটি লুপ তৈরি করে। এতে head এবং tail এর মধ্যে কোন পার্থক্য থাকে না, এবং আপনি লিস্টের শেষে পৌঁছানোর পর পুনরায় প্রথম নোডে ফিরে আসতে পারেন।
এটি সাধারণত এমন পরিস্থিতিতে ব্যবহৃত হয় যেখানে ডাটা একত্রিত করা হয় এবং পুনরাবৃত্তি/সাইক্লিক্যাল অ্যাক্সেস প্রয়োজন। উদাহরণস্বরূপ, round-robin scheduling বা buffer management সিস্টেমে এটি ব্যবহৃত হয়।
1. Circular Linked List এর কাঠামো
একটি সাধারণ Circular Linked List তে নোডগুলো সাধারণত দুটি অংশে বিভক্ত:
- Data: প্রতিটি নোডে যে ডাটা রয়েছে।
- Next: পরবর্তী নোডের রেফারেন্স।
এটি সাধারণ লিঙ্কড লিস্ট থেকে আলাদা কারণ এর last node এর next পয়েন্টার প্রথম নোডের দিকে নির্দেশ করে, যার ফলে এটি সাইক্লিক্যাল হয়ে যায়।
2. Circular Linked List এর সুবিধা
- Looping: এটি লুপিং বা সাইক্লিক্যাল অ্যাক্সেস করতে সাহায্য করে। একবার আপনি লিস্টের শেষ নোডে পৌঁছালে, আপনি আবার প্রথম নোডে ফিরে যেতে পারেন।
- Efficient Traversing: Circular Linked List সাধারণত এমন সিস্টেমে ব্যবহৃত হয় যেখানে ডাটা বারবার রিড বা প্রসেস করার প্রয়োজন হয়।
- No NULL Values: শেষ নোডের পর NULL ভ্যালু থাকে না, বরং এটি সিস্টেমে সাইক্লিক্যাল লুপ তৈরি করে।
3. Circular Linked List এর ব্যবহার
Circular Linked List বিভিন্ন ধরনের প্রোগ্রামে ব্যবহৃত হতে পারে, যেমন:
- Round-Robin Scheduling: এতে প্রতিটি প্রসেসকে সঠিক পরিমাণ সময়ের জন্য প্রসেসিং করার পর, পরবর্তী প্রসেসের জন্য সিরিয়ালভাবে টাস্ক পাঠানো হয়।
- Circular Buffers: ডাটা স্টোরেজের জন্য একটি রিং বাফার ব্যবহার করা হয় যাতে ইনপুট বা আউটপুট সিস্টেম সঠিকভাবে কাজ করতে পারে।
- Gaming Applications: গেমের প্লেয়ারদের মধ্যে টার্ন নেওয়ার জন্য বা ডাটা রাউন্ডরবিনে প্রসেস করার জন্য।
4. Circular Linked List Implementation in Java
এখানে একটি সিম্পল Circular Linked List এর উদাহরণ দেয়া হলো, যেখানে একটি লিঙ্কড লিস্ট তৈরি করা হবে এবং কিছু মৌলিক অপারেশন যেমন ইনসার্ট, ট্র্যাভার্স এবং ডিলিট করা হবে।
4.1. Circular Linked List Class Implementation
class CircularLinkedList {
// Node class
class Node {
int data;
Node next;
// Constructor
public Node(int data) {
this.data = data;
this.next = null;
}
}
Node head = null; // Head node of the list
Node tail = null; // Tail node of the list
// Method to insert a new node at the end
public void insert(int data) {
Node newNode = new Node(data);
// If the list is empty, make the new node the head and tail
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head; // Point the next of the last node to the head
} else {
tail.next = newNode; // Add the new node at the end
tail = newNode; // Update the tail to the new node
tail.next = head; // Point the next of the new tail node to the head
}
}
// Method to traverse and print the list
public void traverse() {
if (head == null) {
System.out.println("List is empty.");
return;
}
Node temp = head;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != head); // Continue until we circle back to the head
System.out.println();
}
// Method to delete a node from the list
public void delete(int data) {
if (head == null) {
System.out.println("List is empty.");
return;
}
Node temp = head;
Node prev = null;
// If the node to be deleted is the head node
if (temp.data == data) {
if (head == tail) { // If there's only one node
head = tail = null;
} else {
head = head.next;
tail.next = head; // Update tail's next pointer to new head
}
System.out.println("Node with data " + data + " deleted.");
return;
}
// Search for the node to be deleted
do {
prev = temp;
temp = temp.next;
} while (temp != head && temp.data != data);
// If node is not found
if (temp == head) {
System.out.println("Node with data " + data + " not found.");
return;
}
// Delete the node
prev.next = temp.next;
if (temp == tail) {
tail = prev; // Update tail if it was the last node
}
System.out.println("Node with data " + data + " deleted.");
}
}
public class Main {
public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
list.insert(50);
System.out.println("Circular Linked List:");
list.traverse();
list.delete(30);
System.out.println("After deleting 30:");
list.traverse();
list.delete(10);
System.out.println("After deleting 10:");
list.traverse();
}
}
ব্যাখ্যা:
- Node Class: একটি
Nodeক্লাস তৈরি করা হয়েছে যার মধ্যে data এবং next পয়েন্টার রয়েছে। - Insert Method:
insert(int data)মেথডটি নতুন Node তৈরি করে এবং তাকে সঠিকভাবে Circular Linked List এ যুক্ত করে। - Traverse Method:
traverse()মেথডটি লিস্টের সমস্ত নোড প্রদর্শন করে, এবং এটি Circular হওয়ায়, head থেকে পুনরায় ঘুরে ফিরে আসে। - Delete Method:
delete(int data)মেথডটি ডাটা অনুসারে একটি নোড মুছে ফেলে। এটি সঠিকভাবে প্রথম, মধ্য এবং শেষ নোড মুছে ফেলার জন্য প্রোগ্রাম করা হয়েছে।
5. Circular Linked List এর অন্যান্য ব্যবহার
- Round-robin Scheduling: এটি একটি সাধারণ ব্যবহার যেখানে ডাটা বা প্রক্রিয়াগুলি একটি নির্দিষ্ট অর্ডারে একে অপরকে সুইচ করে।
- Circular Buffering: সিস্টেমে ডাটা স্টোরেজ যেখানে ডাটা পুনরাবৃত্তি হয় এবং সীমিত জায়গায় সঞ্চিত থাকে।
- Music Playlist: প্লে লিস্টে গান চলার পর প্লে লিস্ট আবার প্রথম গান থেকে শুরু হতে পারে।
সারাংশ
Circular Linked List একটি বিশেষ ধরনের লিঙ্কড লিস্ট যেখানে শেষ নোডটি প্রথম নোডের সাথে যুক্ত থাকে। এটি round-robin scheduling, buffer management, এবং gaming applications এর মতো বিভিন্ন পরিস্থিতিতে ব্যবহৃত হয়। Java তে Circular Linked List তৈরি করার জন্য আমরা Node এবং CircularLinkedList ক্লাস ব্যবহার করেছি, এবং ডাটা ইনসার্ট, ট্র্যাভার্স এবং ডিলিট অপারেশনগুলো দেখানো হয়েছে।
Read more